
class tUser:
    def __init__(self, dbconn):
        self._dbconn = dbconn
        self._logger = dbconn.logger

    def createTable(self):
        if self._dbconn.tableExists('tUser'):
            return True

        sql = """CREATE TABLE tUser (
                 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                 name char (100) not null,
                 password char(100) not null)"""

        if self._dbconn.createTable(sql):
            return True
        self._logger.write('...... failed ... create table: tUser')
        return False

    def isUserExist(self,username, password):
        rtup = (str(username), str(password))
        sql = """SELECT * FROM tUser WHERE name='%s' and password='%s'""" % rtup
        rows = self._dbconn.read(sql)
        if rows is None:
            return False
        return len(rows) > 0

    def createRow(self, name, password):
        sql = "INSERT INTO tUser(name, password) VALUES ( '%s', '%s') " % (name, password)

        if self._dbconn.insertRetId(sql) is None:
            self._logger.write('...... failed ... insert row: tUser')
            print('fail to insert row into tUser')
            return False
        return True
